<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form ref="ruleForm"
             :model="ruleForm"
             :rules="rules"
             label-width="90px"
             label-position="right">
      <el-form-item label="节点名称" prop="label">
        <el-input v-model="ruleForm.label"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addOrUpdateCategory } from '@/api/category'
export default {
  name: 'AddOrUpdateCategory',
  data() {
    return {
      title: '',
      dialogVisible: false,
      ruleForm: {
        id: '',
        parentId: '',
        label: '',
        tableName: '',
      },
      rules: {
        label: [
          { required: true, message: '名称不能为空', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addOrUpdateCategory({
            id: this.ruleForm.id,
            label: this.ruleForm.label,
            tableName: this.ruleForm.tableName,
            parentId: this.ruleForm.parentId
          }).then(() => {
            this.dialogVisible = false
            this.$parent.getTreeList()
            this.handleClose()
          })
        }
      })
    },
    handleClose() {
      this.$parent.AddOrUpdateCategoryVisible = false
    }
  }
}
</script>

<style scoped>

</style>
